import { _decorator, Component, Node, Label, Button } from 'cc';
import { ResMgr } from '../../../Framework/Scripts/Managers/ResMgr';
import { SoundMgr } from '../../../Framework/Scripts/Managers/SoundMgr';
import { UIComponent, UIMgr } from '../../../Framework/Scripts/Managers/UIMgr';
import { GoldType } from '../constant';
import { GameApp } from '../GameApp';
import { UserData } from '../userData/UserData';
const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('GameWin_UICtrl')
export class GameWin_UICtrl extends UIComponent {
    private coin: Label = null
    private ore: Label = null
    private coinNum: number = 0
    private oreNum: number = 0
    upGold() {


        UserData.Instance.localData[GoldType.ore] += this.oreNum
        UserData.Instance.localData[GoldType.gold] += this.coinNum
        UserData.Instance.saveLocalData2()
    }
    async onLoad() {
        GameApp.Instance.isGameOver=true
        SoundMgr.Instance.stopBgMusic()
        SoundMgr.Instance.playSound(ResMgr.Instance.getAsset("Sounds", "win"))

        const nowLevel: number = UserData.Instance.localData["level"] - 1
        if (nowLevel == 1) {
            UIMgr.Instance.showUIView("SkillChoiceUI")
        } else {
            if (nowLevel % 3 == 0) {
                UIMgr.Instance.showUIView("SkillChoiceUI")
            }
        }

        this.coin = this.ViewNode("coin/Label").getComponent(Label)
        this.ore = this.ViewNode("ore/Label").getComponent(Label)
        //计算基础奖励

        this.coinNum += nowLevel * 2 + 300
        this.oreNum += nowLevel + 100

        this.ore.string = "" + this.oreNum
        this.coin.string = "" + this.coinNum
        this.upGold()

 
        this.AddButtonListener("nextLevel", this, () => {
            GameApp.Instance.stopTimer()
            GameApp.Instance.delAll()
            UIMgr.Instance.DestroyUIView(this.node.name)
            GameApp.Instance.StartGame()
        })

        this.AddButtonListener("outBtn", this, () => {
            GameApp.Instance.stopTimer()
            GameApp.Instance.delAll()
            UIMgr.Instance.DestroyUIView(this.node.name)
            GameApp.Instance.enterGame()
        })
    }
    start() {

    }

    update(dt: number) {

    }
}


